from django.http import HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .forms import AddArticleForm, AddCommentForm
from .models import Article, Comment
from mcom.forum_config import BOARDS, is_boardid


COUNT_OF_PAGE = 2

def latest_articles(request):
    article_list = Article.objects.all()
    paginator = Paginator(article_list, COUNT_OF_PAGE)
    page = request.GET.get("page")
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)

    return render(request, "forum/latest_articles.html", {"latest_articles":articles})


def articles_of_board(request):
    boardid = request.GET.get("boardid", "1")
    if not boardid.isdigit():
        return HttpResponse("Invalid request!")

    boardid = int(boardid)
    article_list = Article.objects.filter(boardid=boardid)
    paginator = Paginator(article_list, COUNT_OF_PAGE)
    page = request.GET.get("page")
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)

    return render(request, "forum/articles_of_board.html", {"articles_of_board":articles})


def article(request):
    article_id = int(request.GET.get('id', '0'))
    article = get_object_or_404(Article, pk=article_id)
    article.reads = article.reads + 1
    article.save()
    return render(request, "forum/article.html", {"article":article})
    
@login_required
def add_article(request):
    if request.method == 'POST':
        f = AddArticleForm(request.POST)
        boardid = request.POST.get("boardid", None)
        if boardid == None:
            return HttpResponse("invalid boardid")

        if boardid.isdigit() and is_boardid(int(boardid)):
            boardid = int(boardid)
        else:
            return HttpResponse("invalid boardid")

        if f.is_valid():
            article = f.save(False)
            article.author = request.user
            article.boardid = boardid
            article.save()
            return redirect("/")

    else:
        f = AddArticleForm()
        boards = BOARDS.values() 
        return render(request, "forum/add_article.html", {"form":f, "boards":boards})


@login_required
def add_comment(request):
    if request.method == 'POST':
        article_id = int(request.POST.get("id"))
        article = get_object_or_404(Article, pk=article_id)
 
        f = AddCommentForm(request.POST)
        if f.is_valid():
            comment = f.save(False)
            comment.author = request.user
            comment.article = article
            comment.save()
            article.comments = article.comments + 1
            article.save()
            return redirect("/forum/article?id=%d"%(article_id))
        else:
            return HttpResponse("error")
    else:
        article_id = int(request.GET.get("id"))
        article = get_object_or_404(Article, pk=article_id)
 
        f = AddCommentForm() 
        return render(request, "forum/add_comment.html", {"form":f, "article":article})

